package com.song.osclient;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.loopj.android.http.PersistentCookieStore;
import com.song.osclient.http.Http;
import com.song.osclient.util.PreferenceHelper;
import com.song.osclient.util.PrintLog;

import java.io.File;

/**
 * Created by song on 2015/7/28.
 */
public class App extends Application {
    private static App app;
    private static String appDir;
    public static final String THEME_DAY = "日间";
    public static final String THEME_NIGHT = "夜间";
    private static String currentTheme;

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        init();
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable ex) {
                PrintLog.error(ex.getMessage());
                ex.printStackTrace();
            }
        });
    }

    private void init() {
        initAppDir();
        Http.setCookieStore(new PersistentCookieStore(this));
        currentTheme = PreferenceHelper.getString("theme", THEME_DAY);
    }

    private void initAppDir() {
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            appDir = Environment.getExternalStorageDirectory() + "/osclient/test/";
            File file = new File(appDir);
            if (!file.exists()) file.mkdirs();
        }
    }

    public static App getContext() {
        return app;
    }

    public static String getAppDir() {
        return appDir;
    }

    public static String getCurrentThemeTag() {
        return currentTheme;
    }

    public static int getCurrentTheme() {
        if (getCurrentThemeTag().equals(THEME_DAY)) {
            return R.style.theme_day;
        } else {
            return R.style.theme_night;
        }
    }

    public static void switchTheme() {
        if (getCurrentThemeTag().equals(THEME_DAY)) {
            currentTheme = THEME_NIGHT;
        } else {
            currentTheme = THEME_DAY;
        }
        PreferenceHelper.save("theme", currentTheme);
    }

    public static SharedPreferences getSharePreference() {
        return PreferenceManager.getDefaultSharedPreferences(getContext());
    }


    public static int getScreenWidth() {
        WindowManager windowManager = (WindowManager) app.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            Point point = new Point();
            display.getSize(point);
            return point.x;
        }
        return display.getWidth();
    }


    public static int getScreenHeight() {
        WindowManager windowManager = (WindowManager) app.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            Point point = new Point();
            display.getSize(point);
            return point.y;
        }
        return display.getHeight();
    }


    public static float getDensity() {
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wm = (WindowManager) app.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(dm);
        return dm.density;
    }

    public static int dp2Px(float dp) {
        final float scale = app.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    public static float px2dp(float value) {
        return (value / app.getResources().getDisplayMetrics().density);
    }

    public static void installApk(File apk) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(apk), "application/vnd.android.package-archive");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        app.startActivity(intent);
    }

    public static int getVersionCode() {
        int versionCode;
        try {
            versionCode = app.getPackageManager().getPackageInfo(app.getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException ex) {
            versionCode = 0;
        }
        return versionCode;
    }

    public static String getVersionName() {
        String name;
        try {
            name = app.getPackageManager().getPackageInfo(app.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException ex) {
            name = "";
        }
        return name;
    }
}
